﻿using AutoMapper;
using Microsoft.Extensions.Logging;
using XsqTech.SmartOA.Domain.Table;
using XsqTech.SmartOA.Dtos.DTO;
using XsqTech.SmartOA.EFCore.Respository;
using XsqTech.SmartOA.IRespository;
using XsqTech.SmartOA.IService;
using XsqTech.SmartOA.Service.DTO;

namespace XsqTech.SmartOA.Service
{
    public class RoleService : IRoleService
    {
        readonly IRolesRespository _baseRole;
        private readonly ILogger<UserService> _logger;
        readonly IUnitOfWork _unitOfWork;
        readonly IMapper _mapper;
        public RoleService(IRolesRespository baseRole, ILogger<UserService> logger, IMapper mapper, IUnitOfWork unitOfWork)
        {
            _baseRole = baseRole;
            _logger = logger;
            _mapper = mapper;
            _unitOfWork = unitOfWork;
        }

        public ResponseModel RoleAdd(RolesInputDto inputDto)
        {
            try
            {
                var s = _mapper.Map<Roles>(inputDto);
                _baseRole.Add(s);
                int data = _unitOfWork.SaveChangs();
                if (data > 0)
                {
                    return new ResponseModel
                    {
                        Code = State.请求成功,
                        Message = "添加成功",
                        Count = data,
                    };
                }
                else
                {
                    return new ResponseModel
                    {
                        Code = State.请求失败,
                        Message = "添加失败",
                        Count = data,
                    };
                }
            }
            catch (Exception ex)
            {
                //写入日志
                _logger.LogError("[错误信息]：" + ex.Message);
                throw;
            }
        }

        public dynamic Show()
        {
            var list = from x in _baseRole.Query()
                       select new RolesOutputDto
                       {
                           RId = x.RId,
                           RName = x.RName,
                           RText = x.RText,
                           Status = x.Status
                       };

            return new ResponseModel
            {
                Code = State.请求成功,
                Message = "查询成功",
                Data = list
            };
        }

        /// <summary>
        /// 角色删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ResponseModel RoleDelete(string id )
        {
            try
            {
                var list = _baseRole.Query().FirstOrDefault(x => x.RId.ToString().Equals(id));
                _baseRole.Delete(list.RId);

                int data = _unitOfWork.SaveChangs();
                if (data > 0)
                {
                    return new ResponseModel
                    {
                        Code = State.请求成功,
                        Message = "删除成功",
                        Count = data,
                    };
                }
                else
                {
                    return new ResponseModel
                    {
                        Code = State.请求失败,
                        Message = "删除失败",
                        Count = data,
                    };
                }
            }
            catch (Exception ex)
            {
                //控制台输出
                Console.WriteLine(ex.Message);
                //写入日志
                _logger.LogError(ex.Message);
                throw;
            };
        }
        /// <summary>
        /// 角色添加权限
        /// </summary>
        /// <param name="rid"></param>
        /// <param name="pid"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public int AddRolePermission(string rid, string pid)
        {
            return _baseRole.AddRolePermission(rid, pid);
        }

        /// <summary>
        /// 获取角色对应的权限
        /// </summary>
        /// <param name="rid"></param>
        /// <returns></returns>
        public List<string> GetRolePermission(string rid)
        {
            var PermissionId = from rolePermission in _baseRole.GetRolePermissions(rid)
                               select new
                               {
                                   pid = rolePermission.PermissionId
                               };
            List<string> ids = new List<string>();
            foreach (var id in PermissionId)
            {
                string pId = id.pid.ToString();
                ids.Add(pId);
            }
            return ids;
        }

    }
}
